package com.miyang.service;

import com.miyang.dao.UserDao;
import com.miyang.entity.UserEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import sun.nio.cs.US_ASCII;

import java.util.ArrayList;
import java.util.List;

/**
 * @author shijin.huang
 * @date 2019-07-31
 */
@Slf4j
@Service
public class UserServiceImpl{
    @Autowired
    UserServiceImpl userService;

    @Autowired
    UserBatchService userBatchService;
/*
    @Autowired
    UserDao userDao;
    public UserEntity getUser(Long id){
        return  userDao.getUser(id);
    }*/


    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    public List<UserEntity> getUsers() {
        List<Long> ids=new ArrayList<>();
        ids.add(1L);
        ids.add(2L);
        ids.add(3L);
        List<UserEntity> userEntities = new ArrayList<>();
        for (Long id : ids) {
            UserEntity user = userBatchService.getUser(id);
            userEntities.add(user);
        }
        return userEntities;
    }

 /*   @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.NESTED)
    public void insertUser(UserEntity userEntity){
        userDao.inserUser(userEntity);
    }


    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.NESTED)
    public void inserUsers() throws Exception {
        List<UserEntity> userEntities = new ArrayList<>();

        UserEntity userEntity=new UserEntity();
        userEntity.setPassword("5");
        userEntity.setUserName("5");

        UserEntity userEntity1=new UserEntity();
        userEntity.setPassword("51");
        userEntity.setUserName("51");

        userEntities.add(userEntity);
        userEntities.add(userEntity1);

        for (UserEntity userEntity2 : userEntities) {
            if(userEntity2.getUserName().equals("51")){
               throw  new Exception();
            }
            userService.insertUser(userEntity2);
        }

    }*/

}
